package com.uenpay.im.ui.old.activity.publicnumber;

import com.qiyunxin.android.http.net.HttpRequestType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.invokeitems.HttpRequestHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-19 下午2:40:36
 * @copyRight http://www.qiyunxin.com
 * @function 公众号管理类
 */
public class PublicNumHandle {

	public interface IGetPublicNumMenu {
		void getPublicNumMenuResult(int status, String msg,
											 ArrayList<PublicNumMenu> arrayList);
	};

	/**
	 * 获取公众号Menu
	 */
	public void getPublicNumMenu(final String custid,
			final IGetPublicNumMenu mGetPublicNumMenu) {
		GetPublicNumMenuInvokeItem item = new GetPublicNumMenuInvokeItem(custid);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {
							final GetPublicNumMenuInvokeItem.GetPublicNumMenuInvokeItemResult result = ((GetPublicNumMenuInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								mGetPublicNumMenu.getPublicNumMenuResult(
										result.status, result.msg,
										result.arrayList);
								if (result.status == 0
										&& result.arrayList != null
										&& result.arrayList.size() > 0) {
									// TODO 缓存menu
								}
							} else {
								mGetPublicNumMenu.getPublicNumMenuResult(-1,
										failReason, null);
							}

						} else {
							mGetPublicNumMenu.getPublicNumMenuResult(-1,
									failReason, null);
						}
					}
				});

	}

	public interface IGetPublicListListener {
		void getPublicList(int status, String msg,
									ArrayList<PublicNumEntity> arrayList);
	};

	/**
	 * 获取我的公众号列表
	 *
	 * @param page
	 */
	public void getPublicList(int page,
			final IGetPublicListListener mGetPublicListListener) {
		MyPublicNumberListInvokeItem item = new MyPublicNumberListInvokeItem(
				page);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							MyPublicNumberListInvokeItem.MyPublicNumberListInvokeItemResult result = ((MyPublicNumberListInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								if (mGetPublicListListener != null) {
									mGetPublicListListener.getPublicList(
											result.status, result.msg,
											result.arrayList);
								}
							} else {
								if (mGetPublicListListener != null) {
									mGetPublicListListener.getPublicList(-1,
											failReason, null);
								}
							}
						} else {
							if (mGetPublicListListener != null) {
								mGetPublicListListener.getPublicList(-1,
										failReason, null);
							}
						}
					}
				});

	}

	public interface ISetPublicNumMsgPushListener {
		abstract void onSetPublicNumMsgPushResult(int status, String msg);
	};

	/**
	 * 设置是否接受公众号信息
	 *
	 * @param publicNumId
	 * @param push
	 * @param mSetPublicNumMsgPushListener
	 */
	public void setPublicNumMsgPush(String publicNumId, int push,
			final ISetPublicNumMsgPushListener mSetPublicNumMsgPushListener) {
		SetMsgPublicNumberInvokeItem item = new SetMsgPublicNumberInvokeItem(
				publicNumId, push);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							SetMsgPublicNumberInvokeItem.SetMsgPublicNumberInvokeItemResult result = ((SetMsgPublicNumberInvokeItem) resultObject)
									.getOutput();

							if (result != null) {
								if (mSetPublicNumMsgPushListener != null) {
									mSetPublicNumMsgPushListener
											.onSetPublicNumMsgPushResult(
													result.status, result.msg);
								}
							}
						} else {
							if (mSetPublicNumMsgPushListener != null) {
								mSetPublicNumMsgPushListener
										.onSetPublicNumMsgPushResult(-1,
												failReason);
							}
						}
					}
				});
	}

	/**
	 * 关注公众号
	 *
	 * @param type
	 * @param publicNumId
	 * @param mSetPublicNumMsgPushListener
	 */
	public void followPublicNum(final int type, final String publicNumId,
			final ISetPublicNumMsgPushListener mSetPublicNumMsgPushListener) {
		FollowPublicNumberInvokeItem item = new FollowPublicNumberInvokeItem(
				type, publicNumId);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							FollowPublicNumberInvokeItem.FollowPublicNumberInvokeItemResult result = ((FollowPublicNumberInvokeItem) resultObject)
									.getOutput();

							if (result != null) {
								IMApplication.showToast(result.msg);
								if (mSetPublicNumMsgPushListener != null) {
									mSetPublicNumMsgPushListener
											.onSetPublicNumMsgPushResult(
													result.status,
													result.msg);
								}
							}
						} else {
							if (mSetPublicNumMsgPushListener != null) {
								mSetPublicNumMsgPushListener
										.onSetPublicNumMsgPushResult(-1,
												failReason);
							}
						}
					}
				});

	}

	public interface IGetPublicNumListener {
		void onGetPublicNumResult(int status, String msg,
												  PublicNumEntity publicNumEntity);
	}

	/**
	 * 获取公众号详情
	 *
	 * @param publicid
	 *            公众号id
	 * @param isByCustId
	 *            是否通过用户id类型获取公众号详情
	 * @param mIGetPublicNumListener
	 */
	public void getPublicNumDetail(String publicid, boolean isByCustId,
			final IGetPublicNumListener mIGetPublicNumListener) {
		PublicNumberDetailInvokeItem item = new PublicNumberDetailInvokeItem(
				publicid, isByCustId);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							PublicNumberDetailInvokeItem.PublicNumberDetailInvokeItemResult result = ((PublicNumberDetailInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								mIGetPublicNumListener
										.onGetPublicNumResult(
												result.status, result.msg,
												result.publicNumEntity);
							} else {
								mIGetPublicNumListener
										.onGetPublicNumResult(-1, failReason, null);
							}
						} else {
							if (mIGetPublicNumListener != null) {
								mIGetPublicNumListener
										.onGetPublicNumResult(-1,
												failReason,null);
							}
						}
					}
				});
	}

	public interface ISearchPublicNumListener {
		void onSearchPublicNumResult(int status, String msg,
													 ArrayList<PublicNumEntity> arrayList);
	}

	/**
	 * 搜索公众号
	 *
	 * @param page
	 * @param keyWord
	 * @param mISearchPublicNumListener
	 */
	public void searchPublicNums(int page, String keyWord,
			final ISearchPublicNumListener mISearchPublicNumListener) {
		SearchPublicNumberListInvokeItem item = new SearchPublicNumberListInvokeItem(
				page, keyWord);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							SearchPublicNumberListInvokeItem.SearchPublicNumberListInvokeItemResult result = ((SearchPublicNumberListInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								mISearchPublicNumListener
										.onSearchPublicNumResult(
												result.status, result.msg,
												result.arrayList);
							} else {
								mISearchPublicNumListener
										.onSearchPublicNumResult(-1, failReason,
												null);
							}
						} else {
							if (mISearchPublicNumListener != null) {
								mISearchPublicNumListener
										.onSearchPublicNumResult(-1,
												failReason,null);
							}
						}
					}
				});

	}

}